var createGrid = function(cfg) {
	var keys = cfg.keys.toLowerCase().split(',');
	var fields = [].concat(keys), columns = [];
	for (var i = 0; i < cfg.list.length; i++) {
		var obj = cfg.list[i];
		var col = obj.column_name.toLowerCase();
		fields.push(col);
		var editor = new Ext.form.TextField();
		columns.push({
					dataIndex : col,
					header : obj.comments || col,
					editor : editor,
					width : 180
				});
	}
	var kvs = {};
	var grid = new Ext.grid.EditorGridPanel({
				border : false,
				store : new Ext.data.JsonStore({
							fields : fields,
							url : My.url('/system/common/queryBySql.do'),
							baseParams : {
								sql : 'select * from ' + cfg.tablename
							},
							root : 'data',
							autoLoad : true
						}),
				cm : new Ext.grid.ColumnModel({
							columns : columns
						}),
				sm : new Ext.grid.RowSelectionModel(),
				listeners : {
					beforeedit : function(e) {
						for (var i = 0; i < keys.length; i++) {
							var kv = e.record.data[keys[i]];
							kvs[keys[i]] = kv;
						}
					},
					afteredit : function(e) {
						// 拼主键条件
						var where = '';
						for (var i = 0; i < keys.length; i++) {
							if (where.length > 0) {
								where += ' and ';
							}
							var kv = kvs[keys[i]];
							where += keys[i] + '=\'' + kv + '\'';
						}
						// 处理英文单引号
						var value = (Ext.util.Format.trim(e.value) || '')
								.replace(/'/g, "''");
						// 拼接SQL
						var sql = 'update ' + cfg.tablename + ' set ' + e.field
								+ '=\'' + value + '\' where ' + where;
						My.request({
									loadMask : false,
									url : My.url('/system/common/exec.do'),
									params : {
										sql : sql
									},
									success : function() {
										e.record.commit();
									}
								});
					}
				}
			});
	return grid;
};
Ext.onReady(function() {
			var cfg = Ext.decode(Ext.get("cfg").dom.value || '{}');
			var grid = createGrid(cfg);
			new Ext.Viewport({
						layout : 'fit',
						items : [grid]
					});
		});